Preliminaries


In [1]:
# This line configures matplotlib to show figures embedded in the notebook, 
# instead of opening a new window for each figure. More about that later. 
# If you are using an old version of IPython, try using '%pylab inline' instead.
%matplotlib inline

from pysolve.model import Model
from pysolve.utils import is_close,round_solution

import matplotlib.pyplot as plt

Dependencies

This utilizes the ipywidgets code from https://github.com/jakevdp/ipywidgets

Installation of this is required for the code below to work.

Model SIM


In [2]:
def create_sim_model():
    model = Model()

    model.set_var_default(0)
    model.var('Cd', desc='Consumption goods demand by households')
    model.var('Cs', desc='Consumption goods supply')
    model.var('Gs', desc='Government goods, supply')
    model.var('Hh', desc='Cash money held by households')
    model.var('Hs', desc='Cash money supplied by the government')
    model.var('Nd', desc='Demand for labor')
    model.var('Ns', desc='Supply of labor')
    model.var('Td', desc='Taxes, demand')
    model.var('Ts', desc='Taxes, supply')
    model.var('Y', desc='Income = GDP')
    model.var('YD', desc='Disposable income of households')

    model.param('Gd', desc='Government goods, demand')
    model.param('W', desc='Wage rate')
    model.param('alpha1', desc='Propensity to consume out of income')
    model.param('alpha2', desc='Propensity to consume out of wealth')
    model.param('theta', desc='Tax rate')

    model.add('Cs = Cd')  # 3.1
    model.add('Gs = Gd')  # 3.2
    model.add('Ts = Td')  # 3.3
    model.add('Ns = Nd')  # 3.4
    model.add('YD = (W*Ns) - Ts') # 3.5
    model.add('Td = theta * W * Ns')  # 3.6, theta < 1.0
    model.add('Cd = alpha1*YD + alpha2*Hh(-1)') # 3.7, 0 < alpha2 < alpha1 < 1
    model.add('Hs - Hs(-1) =  Gd - Td')  # 3.8
    model.add('Hh - Hh(-1) = YD - Cd') # 3.9
    model.add('Y = Cs + Gs') # 3.10
    model.add('Nd = Y/W') # 3.11
    
    return model

Interactive Widget


In [3]:
# Generate the steady state solution as a base for our models
steady_state = create_sim_model()
steady_state.set_values({'alpha1': 0.6,
                         'alpha2': 0.4,
                         'theta': 0.2,
                         'Gd': 20,
                         'W': 1})
for _ in xrange(100):
    steady_state.solve(iterations=100, threshold=1e-5)

    prev_soln = steady_state.solutions[-2]
    soln = steady_state.solutions[-1]
    if is_close(prev_soln, soln, rtol=1e-4):
        break


# create a function that will return a figure given a
# parameter value
def plot(param):
    # Create and run the model for the parameter
    model = create_sim_model()
    model.set_values({'alpha1': 0.6,
                      'alpha2': 0.4,
                      'theta': 0.2,
                      'Gd': 20,
                      'W': 1})
    
    model.solutions = steady_state.solutions[-2:]
     
    for _ in xrange(5):
        model.solve(iterations=100, threshold=1e-5)
     
    model.set_values({'alpha1': param/10.0})
     
    for _ in xrange(45):
       model.solve(iterations=100, threshold=1e-5) 
    
    # Grab the data we want to graph
    hdata = [s['Hh'] for s in model.solutions]
    cdata = [s['Cs'] for s in model.solutions]
    vtdata = [s['YD'] for s in model.solutions]

    fig = plt.figure()
    axes = fig.add_axes([0.1, 0.1, 1.3, 1.3])
    axes.tick_params(top='off', right='off')
    axes.spines['top'].set_visible(False)
    axes.spines['right'].set_visible(False)
    axes.set_ylim(0, 250)
    axes.set_xlim(-2, 50)
    axes.yaxis.grid(True)
    
    axes.plot(hdata, linestyle='-', color='g', label='Wealth')
    axes.plot(cdata, linestyle=':', color='r', linewidth=2, label='Consumption')
    axes.plot(vtdata, linestyle='--', color='b', label='Disposable income')
    
    axes.legend(loc=2)
    fig.text(0.9, 0.2, 'alpha1 = ' + str(param/10.0))
    return fig


# Create the widget
from ipywidgets import StaticInteract, RangeWidget, RadioWidget

# Note: It is better to use integer values for the RangeWidget.
# I divide the value of the param by 10 when assiging it to alpha1.
StaticInteract(plot,
               param=RangeWidget(-7, 6, step=1)
               )


Out[3]:
param:

In [ ]: